# 1.布尔类型
ok = (true && false) || true
puts(ok)

num1 = (2 + 3) * (6 / 4)    # 5
puts(num1) 

# 2.字符串与数组切片
str_concat = 'hello' + ' world'
# 此处的slice 是切片，第一个参数表示从第几个开始，
# 后面的一个参数表示切几个,不填写默认为一个
str1 = str_concat.slice(6, 2)
puts str1

arr1 = [0, 1, 2, 3, 4, 5]
sub_arr = arr1.slice(2, 4)
puts sub_arr


# 3. ruby 符号:一般用作hash 表中的键
puts :my_symbol == :another_symbol
# false

puts :my_symbol1 == :my_symbol1
# true

# 特殊值:nil, 代表不存在任何有用的值, 如: "hello".slice(12)
p('hello')
# 4.数组字面量
numbers = ['zero', 'one', 'two']
# puts numbers[1]

numbers.push('three', 'four')
# puts numbers'
# numbers = ['zero', 'one', 'two', 'three', 'four']

number1 = numbers.drop(2)
# numer1 = ['zero', 'one', 'two']
# numbers = ['zero', 'one', 'two', 'three', 'four']
puts number1
# 弹栈 drop里面的参数为弹栈的元素个数 ['two', 'three', 'four']

# 范围
ages = 18..30
ages_arr = ages.entries
puts ages_arr
# ages_arr = [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
puts ages_arr.include?(101)
# false
puts ages_arr.include?(20)
# true

# 5.hash
fruit = {'a'=>'apple', 'b'=>'banana', 'c'=>'coconut', 'o'=>'orange'}
# 读取散列表对应键的值
puts fruit['b']
# banana

# 散列表也可以修改
fruit['h'] = 'haskell'
puts fruit
# {"a"=>"apple", "b"=>"banana", "c"=>"coconut", "o"=>"orange", "h"=>"haskell"}

# 类似于json 格式的hash 写法
dimension = {width:1000, height:2250, depth:250}
puts dimension
# {:width=>1000, :height=>2250, :depth=>250}

# 根据键名读取值
puts dimension[:width]  # 1000

# 根据键名修改值
dimension[:width] = 1111
puts dimension
# {:width=>1111, :height=>2250, :depth=>250}

# 6.proc 即别的语言中的匿名函数/Lambda 表达式
# multiply = (x, y) => {return x * y}
# multiply(6, 9)
# 上述JS ES6代码的 ruby 最紧凑写法如下:

# 使用call 调用
multiply = -> x, y { x * y }
multiply_res = multiply.call(6, 9)
puts multiply_res   # 54

# 使用别的方式调用
multiply_res1 = multiply[6, 9]
puts multiply_res1
